package com.yang.search.mq;

import com.rabbitmq.client.Channel;
import com.yang.common.constant.RabbitMQConstants;
import com.yang.common.model.StandardSubmit;
import com.yang.common.util.JsonUtil;
import com.yang.search.service.SearchService;
import com.yang.search.utils.SearchUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.time.LocalDateTime;

@Component
@Slf4j
public class SmsWriteLogListener {

    @Autowired
    private SearchService searchService;

    //在策略模块是将submit发送过去的，所以也可以用submit接收
    @RabbitListener(queues = RabbitMQConstants.SMS_WRITE_LOG)
    public void consume(StandardSubmit submit, Channel channel, Message message) throws IOException {
        //1、调用搜索模块的添加方法，完成添加操作
        log.info("接收到存储日志的信息   submit = {}",submit);

        //逻辑处理
        searchService.index(SearchUtils.INDEX + SearchUtils.getYear(),submit.getSequenceId().toString(), JsonUtil.objToJson(submit));

        //2、手动ack
        channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
    }

}
